<?php
namespace app\jiaxiaotong\controller;

use cmf\controller\HomeBaseController;
use app\student\model\StudentModel;
use cmf\controller\UserBaseController;
use think\Db;
use think\Request;

class ChildrenInfoController extends UserBaseController
{
    /**
     * 获取家长的学生
     * @return \think\response\Redirect
     */
    private function getStudents()
    {
        $p_id = cmf_get_current_user_id();
        $student_mdl = new StudentModel();
        //获取家长的学生
        $students = $student_mdl->getStudentsByPid($p_id,$this->front_shop_id);
        if(!$students){
            $this->redirect('order/Order/add_student');
        }

        $this->assign('students',$students);
    }

    /**
     * 孩子信息列表
     * @return mixed
     */
    public function index()
    {
        $this->getStudents();
        return $this->fetch();
    }

    public function add()
    {
        $this->getStudents();
        return $this->fetch();
    }

    public function addPost()
    {
        if($this->request->isPost())
        {
            //当前家长
            $p_id = cmf_get_current_user_id();
            if(empty($p_id)){
                //跳转登录
                $this->redirect('user/login/index');
            }

            $student_mdl = new StudentModel();
            $data = $this->request->post();
            $information_sources_type=$data['information_sources_type'];
            if(count($information_sources_type)<1)
            {
                $this->error('没有选择信息来源');
            }

            $information_sources_type=implode(',',$information_sources_type);
            $data['information_sources_type']=$information_sources_type;
            //字段验证
            $result = $this->validate($data,'StudentAdd');
            if ($result !== true) {
                $this->error($result);
            }
            //检查该家长下是否已有该孩子
            $total = $student_mdl
                ->where('shop_id', '=', $this->front_shop_id)
                ->where('delete_time', '=', 0)
                ->where('u_id', '=', $p_id)
                ->where(['name' => trim($data['name'])])
                ->count();
            if (isset($total) && $total >= 1) {
                $this->error('该学生已存在');
            }

            if (!empty($data['birthday'])) {
                //生日
                $data['birthday'] = strtotime($data['birthday']);
            }

            //学号
            $data['code'] = $student_mdl->generateCode();
            $data['school_name'] = trim($data['school_name']);
            $data['name'] = trim($data['name']);
            $data['u_id'] = $p_id;
            $data['create_time'] = time();

            //添加孩子
            $id = $student_mdl->insertGetId($data);
            if ($id) {
//                //记录日志
//                $pk = $id; //主键
//                $table = 'student';//数据表名
//                $content = lang('ADD_STUDENT', ['name' => $data['name']]);
//                do_log($content, $pk, $table, time());

                $this->success('添加成功', 'ChildrenInfo/index', '', 2);
            } else {
                $this->error("添加失败");
            }
        }else{
            $this->error('请求错误');
        }

    }

    public function edit()
    {
        $param = $this->request->param();
        if(!isset($param['id']) || !is_numeric($param['id'])) {
            //跳转到孩子列表
            $this->redirect('ChildrenInfo/index');
        }
        $id = $param['id'];

        $student_mdl = new StudentModel();
        $student = $student_mdl->find($id);
        if(count($student) <= 0){
            //跳转到孩子列表
            $this->redirect('ChildrenInfo/index');
        }

        //获取家长的孩子
        $this->getStudents();

        $this->assign('student',$student);
        return $this->fetch();
    }

    public function editPost()
    {
        if($this->request->isPost())
        {
            //当前家长
            $p_id = cmf_get_current_user_id();
            if(empty($p_id)){
                //跳转登录
                $this->redirect('user/login/index');
            }

            $student_mdl = new StudentModel();
            $data = $this->request->post();
            //字段验证
            $result = $this->validate($data,'StudentAdd');
            if ($result !== true) {
                $this->error($result);
            }

            if (!isset($data['id']) || empty($data['id']) || !is_numeric($data['id'])) {
                $this->error('请求参数有误');
            }

            //检查该家长下是否已有该孩子
            $student = $student_mdl->find($data['id']);
            if (count($student) <= 0) {
                $this->error('该学生不存在');
            }

            if (!empty($data['birthday'])) {
                //生日
                $data['birthday'] = strtotime($data['birthday']);
            }

            //学号
            $data['school_name'] = trim($data['school_name']);
            $data['name'] = trim($data['name']);
            $data['update_time'] = time();

            //编辑孩子
            $res = $student_mdl->editStudent($data);
            if ($res) {
                $this->success('编辑成功');
            } else {
                $this->error("编辑失败");
            }
        }else{
            $this->error('请求错误');
        }
    }
}